import {reactive} from "vue";

const userInfo = reactive({
    username: "张三",
    gender: "男",
    tel: '13455556666'
})

export default {
    namespaced: true,

    state: () => {
        return {
            userInfo: JSON.parse(localStorage.getItem('userInfo')) || {
                gender: "",
                tel: "",
                username: ""
            },
        }

    },

    mutations: {
        SET_USER_INFO(state, userInfo) {
            state.userInfo = userInfo
        },

        SET_PHONE(state, phone) {
            state.userInfo.tel = phone
        }
    },

    actions: {
        getInfo({commit}) {
            commit("SET_USER_INFO", userInfo)
            localStorage.setItem("userInfo", JSON.stringify(userInfo))
        },

        changePhone({commit}, phone) {
            commit("SET_PHONE", phone)
            userInfo.tel = phone
            localStorage.setItem("userInfo", JSON.stringify(userInfo))
        }
    }
}